#
#    Copyright (C) 2012-2018 Carl Hetherington <cth@carlh.net>
#
#    This file is part of DCP-o-matic.
#
#    DCP-o-matic is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    DCP-o-matic is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
#

import os
import subprocess
import shlex
import glob
from waflib import Logs
import i18n

sources = """
          about_dialog.cc
          audio_dialog.cc
          audio_gain_dialog.cc
          audio_mapping_view.cc
          audio_panel.cc
          audio_plot.cc
          barco_alchemy_certificate_panel.cc
          batch_job_view.cc
          check_box.cc
          christie_certificate_panel.cc
          cinema_dialog.cc
          colour_conversion_editor.cc
          config_dialog.cc
          config_move_dialog.cc
          confirm_kdm_email_dialog.cc
          content_colour_conversion_dialog.cc
          content_menu.cc
          content_panel.cc
          content_properties_dialog.cc
          content_sub_panel.cc
          content_view.cc
          controls.cc
          closed_captions_dialog.cc
          dcp_panel.cc
          dcpomatic_button.cc
          email_dialog.cc
          image_sequence_dialog.cc
          isdcf_metadata_dialog.cc
          dcp_text_track_dialog.cc
          dir_picker_ctrl.cc
          dolby_doremi_certificate_panel.cc
          download_certificate_dialog.cc
          download_certificate_panel.cc
          export_dialog.cc
          file_picker_ctrl.cc
          film_editor.cc
          film_name_location_dialog.cc
          film_viewer.cc
          filter_dialog.cc
          filter_editor.cc
          focus_manager.cc
          fonts_dialog.cc
          full_config_dialog.cc
          gain_calculator_dialog.cc
          gdc_certificate_panel.cc
          hints_dialog.cc
          html_dialog.cc
          initial_setup_dialog.cc
          instant_i18n_dialog.cc
          i18n_hook.cc
          job_view.cc
          job_view_dialog.cc
          job_manager_view.cc
          kdm_advanced_dialog.cc
          kdm_cpl_panel.cc
          kdm_dialog.cc
          kdm_output_panel.cc
          kdm_timing_panel.cc
          key_dialog.cc
          make_chain_dialog.cc
          message_dialog.cc
          monitor_dialog.cc
          move_to_dialog.cc
          nag_dialog.cc
          name_format_editor.cc
          new_dkdm_folder_dialog.cc
          normal_job_view.cc
          paste_dialog.cc
          player_config_dialog.cc
          player_information.cc
          playhead_to_timecode_dialog.cc
          playhead_to_frame_dialog.cc
          question_dialog.cc
          recreate_chain_dialog.cc
          repeat_dialog.cc
          report_problem_dialog.cc
          rename_template_dialog.cc
          rgba_colour_picker.cc
          save_template_dialog.cc
          screen_dialog.cc
          screens_panel.cc
          self_dkdm_dialog.cc
          send_i18n_dialog.cc
          server_dialog.cc
          servers_list_dialog.cc
          standard_controls.cc
          static_text.cc
          subtitle_appearance_dialog.cc
          suspender.cc
          system_font_dialog.cc
          table_dialog.cc
          templates_dialog.cc
          text_panel.cc
          text_view.cc
          time_picker.cc
          timecode.cc
          timeline.cc
          timeline_atmos_content_view.cc
          timeline_content_view.cc
          timeline_dialog.cc
          timeline_audio_content_view.cc
          timeline_labels_view.cc
          timeline_text_content_view.cc
          timeline_reels_view.cc
          timeline_time_axis_view.cc
          timeline_video_content_view.cc
          timeline_view.cc
          timing_panel.cc
          update_dialog.cc
          verify_dcp_dialog.cc
          video_panel.cc
          video_waveform_dialog.cc
          video_waveform_plot.cc
          wx_util.cc
          wx_signal_manager.cc
          """

def configure(conf):

    wx_libs = 'core,richtext,adv,html,xml'

    try:
        wx_config = '/usr/lib64/wx/config/gtk2-unicode-3.0'
        conf.check_cfg(msg='Checking for wxWidgets using gtk2-unicode-3.0',
                       package='',
                       path=wx_config,
                       args='--cppflags --cxxflags --libs %s' % wx_libs,
                       uselib_store='WXWIDGETS',
                       mandatory=True)
    except:
        try:
            wx_config = 'wx-config-3.0-gtk2'
            conf.check_cfg(msg='Checking for wxWidgets using wx-config-3.0-gtk2',
                           package='',
                           path=wx_config,
                           args='--cppflags --cxxflags --libs %s' % wx_libs,
                           uselib_store='WXWIDGETS',
                           mandatory=True)
        except:
            wx_config = 'wx-config'
            conf.check_cfg(msg='Checking for wxWidgets using wx-config',
                           package='',
                           path=wx_config,
                           args='--cppflags --cxxflags --libs %s' % wx_libs,
                           uselib_store='WXWIDGETS',
                           mandatory=True)

    if conf.options.static_wxwidgets:
	# wx-config returns its static libraries as full paths, without -l prefixes, which confuses
        # check_cfg().  It puts the static libraries into LINKFLAGS_WXWIDGETS, so fish them out.
        stlibs = []
        new_linkflags = []
        stlib_paths = []
        for f in conf.env.LINKFLAGS_WXWIDGETS:
            if f.startswith('/'):
                d = os.path.dirname(f)
                if not d in stlib_paths:
                    stlib_paths.append(d)
                stlibs.append(os.path.basename(f)[3:-2])
            else:
                new_linkflags.append(f)

        conf.env.STLIB_WXWIDGETS = stlibs
        conf.env.LINKFLAGS_WXWIDGETS = new_linkflags
        conf.env.STLIBPATH_WXWIDGETS = stlib_paths

    conf.in_msg = 1
    wx_version = conf.check_cfg(package='wxwidgets', path=wx_config, args='--version').strip()
    conf.im_msg = 0
    if not wx_version.startswith('3.0.'):
        conf.fatal('wxwidgets version 3.0.x is required; %s found' % wx_version)

    try:
        conf.check_cfg(msg='Checking for RtAudio using pkg-config',
                       package='rtaudio',
                       args='--cflags --libs',
                       uselib_store='RTAUDIO',
                       mandatory=True)
    except:
        conf.check_cfg(msg='Checking for RtAudio headers using rtaudio-config',
                       package='',
                       path='rtaudio-config',
                       args='--cppflags',
                       uselib_store='RTAUDIO',
                       mandatory=True)

        conf.check_cfg(msg='Checking for RtAudio libraries using rtaudio-config',
                       package='',
                       path='rtaudio-config',
                       args='--libs',
                       uselib_store='RTAUDIO',
                       mandatory=True)

    # Some rtaudio-configs don't include rtaudio as a link library.  Go figure.
    conf.env.LIB_RTAUDIO.append('rtaudio')
    if conf.env.TARGET_WINDOWS:
        # Don't explicitly link with pthread on Windows
        conf.env.CFLAGS_RTAUDIO.remove('-pthread')
        conf.env.LINKFLAGS_RTAUDIO.remove('-pthread')
        # We need some libraries for WASAPI
        conf.env.LIB_RTAUDIO.append('mfplat')
        conf.env.LIB_RTAUDIO.append('mfuuid')
        conf.env.LIB_RTAUDIO.append('wmcodecdspuuid')

    conf.check_cxx(fragment="""
                            #include <RtAudio.h>\n
                            int main() { throw RtError("Hello"); }
			    """,
                   msg='Checking for RtError class',
                   use='RTAUDIO',
                   uselib_store='',
                   define_name='DCPOMATIC_USE_RTERROR',
                   mandatory=False)



def build(bld):
    if bld.env.STATIC_DCPOMATIC:
        obj = bld(features='cxx cxxstlib')
    else:
        obj = bld(features='cxx cxxshlib')

    obj.name   = 'libdcpomatic2-wx'
    obj.export_includes = ['..']
    obj.uselib = 'BOOST_FILESYSTEM BOOST_THREAD BOOST_REGEX WXWIDGETS DCP SUB ZIP CXML RTAUDIO '
    if bld.env.TARGET_LINUX:
        obj.uselib += 'GTK '
    if bld.env.TARGET_WINDOWS:
        obj.uselib += 'WINSOCK2 OLE32 DSOUND WINMM KSUSER '
    if bld.env.TARGET_OSX:
        obj.framework = ['CoreAudio']
    obj.use = 'libdcpomatic2'
    if bld.env.VARIANT == 'swaroop':
        obj.source = sources + " swaroop_controls.cc"
    else:
        obj.source = sources
    obj.target = 'dcpomatic2-wx'

    i18n.po_to_mo(os.path.join('src', 'wx'), 'libdcpomatic2-wx', bld)

def pot(bld):
    i18n.pot(os.path.join('src', 'wx'), sources + " editable_list.h content_widget.h", 'libdcpomatic-wx')

def pot_merge(bld):
    i18n.pot_merge(os.path.join('src', 'wx'), 'libdcpomatic-wx')
